import request from "./index.js";
import axios from "axios";

// 密码登录接口
export const userLoginService = (LoginData) => {
    return request.post('/user/login', null, {
        params: {
            phone: LoginData.phone,  // 确保通过查询参数传递 phone
            password: LoginData.password,  // 确保通过查询参数传递 password
            captcha: LoginData.captcha  // 确保通过查询参数传递 captcha
        },
    });
};

// 手机验证码登录接口
export const userLoginByPhoneService = (phone, captcha) => {
    console.log(phone);
    console.log(captcha);
    return request.get('/user/smsLogin', { params: { phone: phone, captcha: captcha } });
}

// 注册接口
export const userRegisterService = (RegisterData, captcha) => {
    return request.post(`/user/register`, RegisterData, { params: { captcha: captcha } });
}

//注销账号
export const userLogoutService = (phone) => {
    return request.post('/user/logout', null, { params: { phone: phone } });
}

// 获取短信验证码接口
export const getConfirmCodeService = (phone) => {
    return axios.get(`/api/sms/send`, { params: { phone: phone } });
}

export const findLostAccountService = (phone, password, captcha) => {
    return request.post(`/user/resetPassword`, null, { params: { phone: phone, password: password, captcha: captcha } });
}
//获取邮箱验证码
export const getEmailConfirmCodeService = (email) => {
    return request.get(`/user/sendEmailVerification`, { params: { email: email } });
}

//更改邮箱
export const updateEmailService = (email, code) => {
    return request.get(`/user/verifyEmail`, { params: { email: email, captcha: code } });
}

//更新用户手机号
export const updatePhoneService = (oldPh, phone, code) => {
    return request.get('/user/changePhone', { params: { oldPhone: oldPh, newPhone: phone, captcha: code } });
}

//上传头像接口获得图像地址
export const uploadAvatarService = (file) => {
    const formData = new FormData();
    formData.append('image', file);
    return request.post('/upload', formData, { headers: { 'Content-Type': 'multipart/form-data' } });
}
// 获取验证码图片接口
export const getLoginImg = async () => {
    const response = await axios('/api/captcha', {
        responseType: 'blob',
        headers: {
            'Content-Type': 'image/jpeg',
        }
    })
    const captchaUrl = URL.createObjectURL(response.data);
    return captchaUrl;
}

// 获取用户信息接口
export const getUserMessage = () => {
    return request.get(`/user/getInfo`);
}

//检查原密码是否正确
export const checkOldPasswordService = (phone, oldPwd) => {
    return request.get('/user/checkPassword', { params: { phone: phone, password: oldPwd } });
}

//更新密码接口
export const updatePasswordService = (phone, newPwd) => {
    return request.get('/user/updatePassword', { params: { phone: phone, password: newPwd } });
}
//新闻排行榜
export const getjournalismRanking = () => {
    return request.get('/news/lastNews');
}

//获取所有新闻类型
export const getAlljourType = () => {
    return request.get('/newsType/list')
}

//新闻资讯页根据条件搜索新闻
export const getJournalismList = (message) => {
    // if(message.sentiment==='all'){
    //     message.sentiment=null;
    // }
    return request.post('/news/rangSearch', message)
}

//多条件查询新闻列表
export const updateJournalismList = (message) => {
    message.keyword = message.searchContent
    if (message.typeId === 0) {
        message.typeId = null;
    }
    if (message.sentiment === 'all') {
        message.sentiment = null;
    }
    if (message.originId === 0) {
        message.originId = null;
    }
    return request.post('/news/conditionsSearch', message)
}

//获取所有新闻来源
export const getJournalismOriginList = () => {
    return request.get('/news/getOriginList');
}

//用户自选股查询
export const getOptionalStockList = (message) => {
    return request.post('/watchlist/stockInfoList', message);
}

//新闻详情页
export const getJournalismDetail = (jourId) => {
    return request.get('/news/newsDetail', { params: { id: jourId } })
}

//论坛列表
export const getForumList = (page, size, column, searchContent) => {
    let searchAndOrder = {
        search: {
            title: searchContent,
            content: searchContent
        },
        orders: [
            {
                column: column,
                isAsc: false
            }
        ]
    }
    return request.post('/forum/pages', searchAndOrder, {
        params: {
            page: page,
            size: size
        }
    });
}

//获得论坛文章点赞情况
export const getForumLikeUser = (forumId) => {
    return request.get('/forum/userIds', { params: { forumId: forumId } });
}

//论坛详情页
export const getForumDetail = (id) => {
    return request.get('/forum/getInfo', { params: { id: id } });
}

//论坛发帖
export const postForum = (forumPost) => {
    return request.post('/forum/publish', forumPost);
}

//论坛评论列表
export const getComments = (id) => {
    return request.get('/comment/list', { params: { postId: id } });
}

//论坛点赞或取消点赞
export const likeForum = (postId) => {
    return request.post(`/forum/like/${postId}`);
}


// 获取与股票相关最新三条新闻
export const listThreeNewsApi = (id) => {
    return request.get("/news/listThreeNews/" + id)
}

// 更新用户信息接口
export const updateUserInfo = (user) => {
    return request.post('/user/updateUserInfo', user); // 确保路径与后端一致
};

// 获取搜索新闻列表
export const getSearchNewsApi = (keyword) => {
    return request.get("/news/searchByKeyWord", {
        params: {
            keyword: keyword
        }
    })
}

//获取搜索股票列表
export const getSearchStockApi = (keyword) => {
    return request.get('/stock/getSearchStock', { params: { keyword: keyword } });
}

